package com.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class TestView extends View {
    private Paint txPaint;
    private Paint shapePaint;
    private int mWidth, mHeight;
    private int[] mColors = {0xff6c5b7b, 0xFF35AE7D, 0xfff67280, 0xffadae7d, 0xff7f8cc4, 0xff52396d, 0xff457c9c};
    private Picture mPicture = new Picture();

    public TestView(Context context) {
        this(context, null);
    }

    public TestView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TestView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPain();
        recording();
    }

    private void initPain() {
        txPaint = new Paint();
        txPaint.setColor(Color.BLACK);       //设置画笔颜色
        txPaint.setStyle(Paint.Style.FILL);  //设置画笔模式为填充
        txPaint.setStrokeWidth(10f);         //设置画笔宽度为10px

        shapePaint = new Paint();
        shapePaint.setColor(Color.GRAY);       //设置画笔颜色
        shapePaint.setStyle(Paint.Style.STROKE);  //设置画笔模式为填充
        shapePaint.setStrokeWidth(5f);         //设置画笔宽度为10px
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }


    public void recording() {
        Canvas canvas = mPicture.beginRecording(500, 500);
        // 创建一个画笔
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.FILL);

        // 在Canvas中具体操作
        // 位移
        canvas.translate(250, 250);
        // 绘制一个圆
        canvas.drawCircle(0, 0, 100, paint);

        mPicture.endRecording();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //        canvas.drawPoint(200,200,txPaint);
        //        canvas.drawPoints(new float[]{400,400,500,500},txPaint);

        canvas.translate(mWidth / 2, mHeight / 2 - 400);// 将画布坐标原点移动到中心位置

        //        canvas.drawLine(500, 100, 1000, 100, txPaint);
        //        canvas.drawLine(200, 500, 1000,200, txPaint);
        //        canvas.drawLines(new float[]{400, 400, 1500, 1500}, txPaint);
        //        Rect rect = new Rect(200,200,100,500);
        //        canvas.drawRect(rect,txPaint);

        //        RectF rect = new RectF(200, 200, 1000, 500);
        //        canvas.drawRect(rect,shapePaint);

        //        canvas.drawRoundRect(rect,20,20,shapePaint);


        drawRound(canvas);


        //        canvas.drawPicture(mPicture,new RectF(0,0,mPicture.getWidth(),mPicture.getHeight()));

    }

    private void drawRound(Canvas canvas) {
        float r = (float) (Math.min(mWidth, mHeight) / 6);  // 饼状图半径
        RectF rectF = new RectF(-r, -r, r, r);
        //        canvas.drawRect(rectF,shapePaint);
        txPaint.setColor(mColors[0]);
        canvas.drawArc(rectF, 0, 100, true, txPaint);

        txPaint.setColor(mColors[2]);
        canvas.drawArc(rectF, 100, 23, true, txPaint);

        txPaint.setColor(mColors[4]);
        canvas.drawArc(rectF, 123, 5, true, txPaint);
        txPaint.setColor(mColors[1]);
        canvas.drawArc(rectF, 128, 70, true, txPaint);
        txPaint.setColor(mColors[5]);
        canvas.drawArc(rectF, 198, 162, true, txPaint);
    }
}
